package thread;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Author:  LiuTao
 * Version: 1.0
 * Date:    2023-11-29  20:30
 */
public class ThreadDemo18 {
    public static void main(String[] args) throws InterruptedException {
        Object A = new Object();
        Object B = new Object();
        Thread thread1 = new Thread(() -> {
            synchronized(A) {
                System.out.println("获取线程A！");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (B) {
                    System.out.println("thread1锁住俩个对象");
                }
            }
        });
        Thread thread2 = new Thread(() -> {
            synchronized(B) {
                System.out.println("获取线程B！");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (A) {
                    System.out.println("thread2锁住俩个对象");
                }
            }
        });

        thread1.start();
        thread2.start();
        thread1.join();
        thread2.join();


    }
}
